Page({
  //记得添加合法域名https://a.hecdn.net
    data: {
      weatherInfo: {
        one: {
            city: '',
            maxTemp: '',
            minTemp: '',
            windDir: '',
            condition: '',
            humidity: '',
            uvIndex: '',
            cloudCover: '',
            windSpeed: '',
            visibility: '',
            altitude: '',
            icon:''
        },
        two: {
            maxTemp: '',
            minTemp: '',
            windDir: '',
            condition: '',
            icon: ''
        },
        three: {
            maxTemp: '',
            minTemp: '',
            windDir: '',
            condition: '',
            icon: ''
        },
        four: {
            maxTemp: '',
            minTemp: '',
            windDir: '',
            condition: '',
            icon: ''
        },
        five: {
          maxTemp: '',
          minTemp: '',
          windDir: '',
          condition: '',
          icon: ''
      }
      },
      date: '',//日期
      dateInfo: {
        day3: '',
        day4: ''
      },
      isRefreshing: false,
      rotateAngle: 0
    },
  
    onLoad() {
      this.getWeather();
      this.setDateInfo();
      console.log("今天是星期几:",this.data.date)
    },
  
    getWeather() {
      return new Promise((resolve, reject) => {
        const that = this;
        wx.request({
          url: 'https://devapi.qweather.com/v7/weather/7d?location=101270101&key=3aef96f44da54094bb31b175c399272d',
          method: "GET",
          success(res) {
              console.log("天气数据:", res.data);  // 添加日志查看返回数据
              
              // 添加图标代码检查和默认值
              const getIconCode = (iconDay) => {
                return iconDay ? iconDay : '100';  // 如果没有图标代码，默认使用晴天图标
              };

              that.setData({
                weatherInfo: {
                  one: {
                    city: '成都', 
                    maxTemp: res.data.daily[0].tempMax,
                    minTemp: res.data.daily[0].tempMin,
                    windDir: res.data.daily[0].windDirDay,
                    condition: res.data.daily[0].textDay,
                    icon: getIconCode(res.data.daily[0].iconDay),
                    humidity: res.data.daily[0].humidity,
                    uvIndex: res.data.daily[0].uvIndex,
                    cloudCover: res.data.daily[0].cloud,
                    windSpeed: res.data.daily[0].windSpeedDay,
                    visibility: res.data.daily[0].vis,
                    altitude: ''
                },
                two: {
                    maxTemp: res.data.daily[1].tempMax,
                    minTemp: res.data.daily[1].tempMin,
                    windDir: res.data.daily[1].windDirDay,
                    condition: res.data.daily[1].textDay,
                    icon: getIconCode(res.data.daily[1].iconDay)
                },
                three: {
                    maxTemp: res.data.daily[2].tempMax,
                    minTemp: res.data.daily[2].tempMin,
                    windDir: res.data.daily[2].windDirDay,
                    condition: res.data.daily[2].textDay,
                    icon: getIconCode(res.data.daily[2].iconDay)
                },
                four: {
                    maxTemp: res.data.daily[3].tempMax,
                    minTemp: res.data.daily[3].tempMin,
                    windDir: res.data.daily[3].windDirDay,
                    condition: res.data.daily[3].textDay,
                    icon: getIconCode(res.data.daily[3].iconDay)
                },
                five: {
                    maxTemp: res.data.daily[4].tempMax,
                    minTemp: res.data.daily[4].tempMin,
                    windDir: res.data.daily[4].windDirDay,
                    condition: res.data.daily[4].textDay,
                    icon: getIconCode(res.data.daily[4].iconDay)
                }
                }
              });
              
              console.log("处理后的天气数据：", that.data.weatherInfo);  // 添加日志查看处理后的数据
              
              that.saveWeatherToCloud();
              resolve();
          },
          fail(err) {
            console.error('天气信息获取失败', err);
            reject(err);
          }
        });
      });
    },
      
    saveWeatherToCloud() {
        const weatherInfo = this.data.weatherInfo; 
        wx.cloud.callFunction({
          name: 'uploadWeatherInfo',
          data: {
            weatherInfo: weatherInfo
          },
          success: res => {
            console.log("数据保存成功：", res.result);
          },
          fail: err => {
            console.error("云函数调用失败：", err);
            wx.showToast({
              title: '云函数调用失败',
              icon: 'error'
            });
          }
        });
      },
      // 获取日期
      setDateInfo() {
        const now = new Date();
        const year = now.getFullYear();
        const month = now.getMonth() + 1; 
        const day = now.getDate();
        const week = now.getDay();
    
        const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        const weekDay = weekDays[week];
    
        const formattedDate = `${weekDay}, ${month}/${day}`;
        // 计算大后天和大大后天的日期
        const day3 = new Date();
        day3.setDate(now.getDate() + 3);
        const day4 = new Date();
        day4.setDate(now.getDate() + 4);
    
        this.setData({
          date: formattedDate,
          dateInfo: {
            day3: day3.getDate(),
            day4: day4.getDate()
          }
        });
      },
      // 刷新方法
      refresh() {
        if (this.data.isRefreshing) return;

        this.setData({
          isRefreshing: true
        });

        // 创建旋转动画
        const animation = wx.createAnimation({
          duration: 2000,  
          timingFunction: 'linear'
        });

        animation.rotate(1080).step();
        this.setData({
          refreshAnimation: animation.export()
        });

        const rotateInterval = setInterval(() => {
          animation.rotate(3600).step();  
          this.setData({
            refreshAnimation: animation.export()
          });
        }, 2000);  

        // 调用获取天气数据的方法
        this.getWeather().then(() => {
          setTimeout(() => {  // 添加延时，让动画多转一会
            clearInterval(rotateInterval);
            this.setData({
              isRefreshing: false,
              refreshAnimation: wx.createAnimation().rotate(0).step().export()
            });
            
            wx.showToast({
              title: '刷新成功',
              icon: 'success',
              duration: 1500
            });
          }, 1000);  // 延迟1秒停止
        }).catch(err => {
          console.error('刷新失败：', err);
          clearInterval(rotateInterval);
          this.setData({
            isRefreshing: false,
            refreshAnimation: wx.createAnimation().rotate(0).step().export()
          });
          
          wx.showToast({
            title: '刷新失败',
            icon: 'error',
            duration: 1500
          });
        });
      }
  });
  